//
//  BCHttpManager.m
//  BCShop
//
//  Created by sheweicai on 15/6/1.
//  Copyright (c) 2015年 美丽点击. All rights reserved.
//

#import "BCHttpManager.h"
#import "NSObject+MJKeyValue.h"
#import "CommonUtilityjudge.h"

#import "SVProgressHUD.h"

@implementation BCHttpManager

+ (instancetype)sharedManager {
    static BCHttpManager *shared_manager = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        shared_manager = [[self alloc] init];
    });
    return shared_manager;
}

#pragma mark Login
- (void)request_Login_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Post andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
        id resultData = [data valueForKeyPath:@"data"];
        if (resultData) {
            block(resultData, nil);
        }else{
            block(nil, error);
        }
    }];
}



#pragma mark category list
- (void)request_GetCategoryListWithId:(NSString *)categoryId complete:(void (^)(id, NSError *))complete {
    NSAssert(categoryId != nil, @"分类Id不能为空");
    NSDictionary *params = @{@"id":categoryId};
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error,AFHTTPRequestOperation *operation) {
        id resultData = [data objectForKey:@"category"];
        if (resultData) {
            complete(resultData, nil);
        }else{
            complete(nil, error);
        }
    }];
}

- (void)request_GetRecommendBrands:(NSString *)brandType iDisplayStart:(NSString *)iDisplayStart iDisplayLength:(NSString *)iDisplayLength complete:(void (^)(id, NSError *))complete {
    NSAssert(brandType != nil, @"brandType不能为空");
    NSAssert(iDisplayStart != nil, @"iDisplayStart不能为空");
    NSAssert(iDisplayLength != nil, @"iDisplayLength不能为空");
    NSDictionary *params = @{
                             @"brandType":brandType,
                             @"iDisplayStart":iDisplayStart,
                             @"iDisplayLength":iDisplayLength
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error,AFHTTPRequestOperation *operation) {
        id resultData = [data objectForKey:@"BrandList"];
        if (resultData) {
            NSLog(@"此处可以待修改");
//            NSArray *arrs=[BCBrandModel objectArrayWithKeyValuesArray:resultData];
//            complete(arrs, nil);
        }else{
            complete(nil, error);
        }
    }];
}


- (void)request_GetDiscountProductWithStatus:(NSString *)status iDisplayStart:(NSString *)iDisplayStart iDisplayLength:(NSString *)iDisplayLength complete:(void (^)(id, NSError *))complete {
    NSAssert(status != nil, @"status不能为空");
    NSAssert(iDisplayStart != nil, @"iDisplayStart不能为空");
    NSAssert(iDisplayLength != nil, @"iDisplayLength不能为空");
    NSDictionary *params = @{
                             @"status":status,
                             @"iDisplayStart":iDisplayStart,
                             @"iDisplayLength":iDisplayLength
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error,AFHTTPRequestOperation *operation) {
        id resultData = [data objectForKey:@"productList"];
        if (resultData) {
//此处需修改
//            NSArray *arrs=[BCDiscountProductModel objectArrayWithKeyValuesArray:resultData];
            NSArray *arrs=[[NSArray alloc] init];
            complete(arrs, nil);
        }else{
            complete(nil, error);
        }
    }];
}

- (void)request_GetNewProductWithiDisplayStart:(NSString *)iDisplayStart iDisplayLength:(NSString *)iDisplayLength complete:(void (^)(id, NSError *))complete {
    NSAssert(iDisplayStart != nil, @"iDisplayStart不能为空");
    NSAssert(iDisplayLength != nil, @"iDisplayLength不能为空");
    NSDictionary *params = @{
                             @"iDisplayStart":iDisplayStart,
                             @"iDisplayLength":iDisplayLength
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error,AFHTTPRequestOperation *operation) {
        id resultData = [data objectForKey:@"productList"];
        if (resultData) {
            complete(resultData, nil);
        }else{
            complete(nil, error);
        }
    }];
}

-(void)request_queryAllProductListWithParams:(id)params andBlock:(void (^)(id, NSError *))block
{
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
        NSLog(@"kNetPath_QueryAllProductLis is %@,error is %@",data,error);
        id result= [data objectForKey:@"productList"];
        if (result) {
//          此处需修改
//            NSArray *arrs=[BCNewProductModel objectArrayWithKeyValuesArray:result];
            NSArray *arrs=[[NSArray alloc] init];
            block(arrs,nil);
        }
        else
        {
            block(nil,error);
        }
    }];
}


-(void)request_queryProductInfoWithProductId:(id)productId complete:(void (^)(id, NSError *))complete 
{ 
    NSDictionary *params = @{
                             @"id":productId
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
//        NSLog(@"kNetPath_QueryAllProductLis is %@,error is %@",data,error);
        id resultData= [data objectForKey:@"product"];
        if (resultData) {
            complete(resultData, nil);
        }else{
            complete(nil, error);
        }
    }];
}

-(void)request_queryProductDiscussionListWithProductId:(id)productId storeId:(id)storeId iDisplayStart:(NSString *)iDisplayStart iDisplayLength:(int)iDisplayLength complete:(void (^)(id, NSError *))complete
{
    NSAssert(iDisplayStart != nil, @"iDisplayStart不能为空");
    NSDictionary *params = @{
                             @"iDisplayStart":iDisplayStart,
                             @"iDisplayLength":[NSString stringWithFormat:@"%i",iDisplayLength],
                             @"storeProductId":productId,
                             @"storeId":storeId
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {

//        NSLog(@"kNetPath_QueryCOMMENTLIST is %@,error is %@",data,error);
        id resultData= [data objectForKey:@"pageResult"];
        if (resultData) {
            complete(resultData, nil);
        }else{
            complete(nil, error);
        }
    }];
}

-(void)request_shoppingCartListWithProductIds:(NSString *)productIds complete:(void (^)(id, NSError *))complete{
    NSDictionary *params = @{
                             @"productIds":productIds
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Post andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
        
        NSLog(@"kNetPath_QueryCOMMENTLIST is %@,error is %@",data,error);
        id resultData= [data objectForKey:@"productList"];
        if (resultData) {
            complete(resultData, nil);
        }else{
            complete(nil, error);
        }
    }];
}

-(void)request_findAddressList:(NSString *)memberId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"memberId":memberId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        
        id resultData= [jsonResponse objectForKey:@"deliverAddress"];
        if (!arrayIsEmpty(resultData)) {
            NSMutableArray *list = [[NSMutableArray alloc] init];
            for (NSInteger i = 0; i<[resultData count]; i++) {
//                NSDictionary *dataInfo = [resultData objectAtIndex:i];
                
//                BCAddressModel *address = [[BCAddressModel alloc] init];
//                address.addressId = [dataInfo objectForKey:@"id"];
//                address.province = [dataInfo objectForKey:@"province"];
//                address.city = [dataInfo objectForKey:@"city"];
//                address.area = [dataInfo objectForKey:@"area"];
//                address.addressDetail = [dataInfo objectForKey:@"addressDetail"];
//                address.phoneCode = [dataInfo objectForKey:@"phoneCode"];
//                address.consignee = [dataInfo objectForKey:@"consignee"];
//                address.sign = [[dataInfo objectForKey:@"sign"] intValue];
//                [list addObject:address];
            }
            complete(list);
        }else{
            complete(nil);
        }
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
    
}

-(void)request_saveAddress:(NSString *)memberId consignee:(NSString *)consignee province:(NSString *)province city:(NSString *)city area:(NSString *)area addressDetail:(NSString *)addressDetail mobile:(NSString *)mobile complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"memberId":memberId,
                             @"consignee":consignee,
                             @"province":province,
                             @"city":city,
                             @"area":area,
                             @"addressDetail":addressDetail,
                             @"phoneCode":mobile,
                             };
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];

}

-(void)request_removeAddress:(NSString *)addressId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"id":addressId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];

}

-(void)request_setDefaultUserAddress:(NSString *)addressId memberId:(NSString *)memberId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"id":addressId,
                             @"memberId":memberId
                             };

    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
}

-(void)request_getUserDefaultAddress:(NSString *)memberId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)errorBlock{
    NSDictionary *params = @{
                             @"memberId":memberId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Post andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
        
//        NSLog(@"kNetPath_QueryCOMMENTLIST is %@,error is %@",data,error);
        id resultData= [data objectForKey:@"signAddress"];
        if (!dictionaryIsEmpty(resultData)) {
//            BCAddressModel *address = [[BCAddressModel alloc] init];
//            address.addressId = [resultData objectForKey:@"id"];
//            address.province = [resultData objectForKey:@"province"];
//            address.city = [resultData objectForKey:@"city"];
//            address.area = [resultData objectForKey:@"area"];
//            address.addressDetail = [resultData objectForKey:@"addressDetail"];
//            address.phoneCode = [resultData objectForKey:@"phoneCode"];
//            address.consignee = [resultData objectForKey:@"consignee"];
//            address.sign = [[resultData objectForKey:@"sign"] intValue];
//            complete(address);
        }else{
            errorBlock(error);
        }
    }];
    
    
//    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:kNetPath_Find_Default_Address withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
//        
//        id resultData= [jsonResponse objectForKey:@"signAddress"];
//        if (!dictionaryIsEmpty(resultData)) {
//            BCAddressModel *address = [[BCAddressModel alloc] init];
//            address.addressId = [resultData objectForKey:@"id"];
//            address.province = [resultData objectForKey:@"province"];
//            address.city = [resultData objectForKey:@"city"];
//            address.area = [resultData objectForKey:@"area"];
//            address.addressDetail = [resultData objectForKey:@"addressDetail"];
//            address.phoneCode = [resultData objectForKey:@"phoneCode"];
//            address.consignee = [resultData objectForKey:@"consignee"];
//            address.sign = [[resultData objectForKey:@"sign"] intValue];
//            complete(address);
//        }else{
//            errorBlock(nil);
//        }
//    } onError:^(NSError *errorInfo){
//        errorBlock(errorInfo);
//    }];
    
}

-(void)request_queryOrderListByStatus:(NSNumber *)status memberId:(NSString *)memberId pageNo:(NSInteger)pageNo pageSize:(NSInteger)pageSize complete:(void (^)(id, NSError *))complete{
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:pageSize],@"pageSize", [NSNumber numberWithInteger:pageNo],@"pageNo",memberId,@"memberId",nil];
    if (!numberIsEmpty(status)) {
        [params setValue:status forKey:@"status"];
    }
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
//        NSLog(@"kNetPath_Find_Order_By_Status is %@,error is %@",data,error);
        
        if (data) {
            complete(data, nil);
        }else{
            complete(nil, error);
        }
    }];
}


-(void)request_queryOrderListByStatus:(NSNumber *)status memberId:(NSString *)memberId pageNo:(NSInteger)pageNo pageSize:(NSInteger)pageSize complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:pageSize],@"pageSize", [NSNumber numberWithInteger:pageNo],@"pageNo",memberId,@"memberId",nil];
    if (!numberIsEmpty(status)) {
        [params setValue:status forKey:@"status"];
    }
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
}

-(void)request_SaveOrder:(NSString *)memberId addressId:(NSString *)addressId discountAmount:(CGFloat)discountAmount productList:(NSArray *)productList complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    
    NSDictionary *dit = @{
                          @"productList":productList,
                          @"discountAmount":[NSNumber numberWithFloat:discountAmount],
                          @"addressId":addressId,
                          @"memberId":memberId
                          };
    NSDictionary *params = @{
                             @"order":@"order"
                             //@"order":[dit JSONString]
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];

    
}

-(void)request_DeleteOrder:(NSString *)ordereId orderStatus:(NSNumber *)orderStatus complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"orderStatus":orderStatus,
                             @"ordereId":ordereId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
}


-(void)request_get_Order_Detail:(NSString *)ordereId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)errorBlock{
    NSDictionary *params = @{
                             @"orderId":ordereId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Get andBlock:^(id data, NSError *error, AFHTTPRequestOperation *operation) {
//        NSLog(@"kNetPath_Find_Order_By_Status is %@,error is %@",data,error);
        
        if (data) {
            complete(data);
        }else{
            errorBlock(error);
        }
    }];
    
    
    
//    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:kNetPath_Find_Order_By_Id withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
//        complete(jsonResponse);
//    } onError:^(NSError *errorInfo){
//        errorBlock(errorInfo);
//    }];
    
}

-(void)request_getMyBalance:(NSString *)memberId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"memberId":memberId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:NO showNetworkErrorAlert:NO onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
    
}

-(void)request_ChangeOrderStatus:(NSString *)memberId makeAmount:(NSNumber *)makeAmount payStatus:(NSNumber *)payStatus payTime:(NSString *)payTime payId:(NSString *)payId tradeNo:(NSString *)tradeNo complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *orderStatusParam = @{
                             @"makeAmount":makeAmount,
                             @"payStatus":payStatus,
                             @"payId":payId,
                             @"memberId":memberId,
                             @"tradeNo":tradeNo,
                             @"payTime":payTime
                             };
    NSDictionary *params = @{
                             @"aliAppoder":@"appoder"
                            // @"aliAppOder":[orderStatusParam JSONString]
                             };
    
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:NO onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
}

-(void)request_Create_New_Order_Pay:(NSString *)orderId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"orderId":orderId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
    
}

-(void)request_searchOrderListByorderId:(NSString *)orderId memberId:(NSString *)memberId pageNo:(NSInteger)pageNo pageSize:(NSInteger)pageSize complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"pageSize":[NSNumber numberWithInteger:pageSize],
                             @"pageNo":[NSNumber numberWithInteger:pageNo],
                             @"memberId":memberId,
                             @"orderId":orderId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Get showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];

}

-(void)request_ConfirmOrder:(NSString *)orderId complete:(RequestCompleteBlock)complete error:(RequestErrorBlock)error{
    NSDictionary *params = @{
                             @"orderId":orderId
                             };
    
    [[BCNetAPIClient sharedJsonClient] requestJsonDataWithUrl:@"" withParams:params withMethodType:Post showProgress:YES showNetworkErrorAlert:YES onCompletion:^(id jsonResponse){
        complete(jsonResponse);
    } onError:^(NSError *errorInfo){
        error(errorInfo);
    }];
        
}

@end
